/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.mail;


import yhao.infra.feature.mail.core.sender.MailSender;
import yhao.infra.feature.mail.core.sender.MailSenderDefinition;
import yhao.infra.feature.mail.core.sender.MailSenderPool;

import java.util.ArrayList;
import java.util.List;

/**
 * 邮件实现工厂类
 *
 * @author yoara
 */
public class ConfigFactory {
    /**生成邮件发送bean配置邮件**/
    public static MailSenderPool makeSenderPool(String senderKey, MailSenderDefinition... definitions){
        MailSenderPool senderPool = new MailSenderPool();
        senderPool.setSenderKey(senderKey);
        List<MailSender> list = new ArrayList<>();
        for(MailSenderDefinition def:definitions){
            MailSender sender = new MailSender();
            sender.setDefaultEncoding("utf-8");
            sender.setHost(def.getHost());
            sender.setPort(def.getPort());
            sender.setUsername(def.getUsername());
            sender.setPassword(def.getPassword());
            sender.setSenderCode(def.getSenderName()+"<"+def.getUsername()+">");
            sender.setJavaMailProperties(def.getProperties());
            list.add(sender);
        }
        senderPool.setMailSenderList(list);

        return senderPool;
    }
}
